package com.wen.service.impl;

import com.github.pagehelper.PageHelper;
import com.wen.dao.RoleMapper;
import com.wen.pojo.Permission;
import com.wen.pojo.Role;
import com.wen.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAll(int page, int size) throws Exception {
        PageHelper.startPage(page, size);
        return roleMapper.findAll();
    }

    @Override
    public int add(Role role) throws Exception {
        return roleMapper.add(role);
    }

    @Override
    public Role findById(int id) throws Exception {
        return roleMapper.findById(id);
    }

    @Override
    public int deleteById(int id) throws Exception {
        return roleMapper.deleteById(id);
    }

    @Override
    public int updateById(Role role) throws Exception {
        return roleMapper.updateById(role);
    }

    @Override
    public List<Permission> findOtherPermissions(int roleId) {
        return roleMapper.findOtherPermissions(roleId);
    }

    @Override
    public void addPermissionToRole(int roleId, Integer[] permissionIds) {
        for (int permissionId : permissionIds) {
            roleMapper.addPermissionToRole(roleId, permissionId);
        }

    }
}
